{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## python基础语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#1.基础操作\n",
    "age = 20  \t\t# 声明一个变量age 用来存储一个数字 20\n",
    "1+1\t\t        # 基础数学加法\n",
    "print('Hello World!')   # 打印Hello World!\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#2.条件判断if\n",
    "if 1 == 2: # 如果 if 跟随的条件为 假 那么不执行属于if 的语句,然后寻找 else\n",
    "    print(\"假的\")\n",
    "else: # 寻找到 else 之后 执行属于else中的语句\n",
    "    print(\"1==2是假的\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#3.循环操作---for\n",
    "for i in range(5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#3.循环操作---while\n",
    "sum = 0\n",
    "n = 99\n",
    "while n > 0:\n",
    "    sum = sum + n\n",
    "    n = n - 1\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "#4.break、continue、pass\n",
    "#break语句可以跳出 for 和 while 的循环体\n",
    "n = 1\n",
    "while n <= 100:\n",
    "    if n > 10:\n",
    "        break\n",
    "    print(n)\n",
    "    n += 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "#continue语句跳过当前循环，直接进行下一轮循环\n",
    "n = 1\n",
    "while n < 10:\n",
    "    n = n + 1\n",
    "    if n % 2 == 0:\n",
    "        continue\n",
    "    print(n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R\n",
      "pass\n",
      "o\n",
      "pass\n",
      "o\n",
      "m\n"
     ]
    }
   ],
   "source": [
    " #pass是空语句，一般用做占位语句，不做任何事情\n",
    " for letter in 'Room':\n",
    "    if letter == 'o':\n",
    "        pass\n",
    "        print('pass')\n",
    "    print(letter)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "#5.数据类型---Number(数字)\n",
    "#Python支持int, float, complex三种不同的数字类型\n",
    "a = 3\n",
    "b = 3.14\n",
    "c = 3 + 4j\n",
    "print(type(a), type(b), type(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b 输出结果： HelloPython\n"
     ]
    }
   ],
   "source": [
    "#5.数据类型---String（字符串）\n",
    "#支持字符串拼接、截取等多种运算\n",
    "a = \"Hello\"\n",
    "b = \"Python\"\n",
    "print(\"a + b 输出结果：\", a + b)\n",
    "#print(\"a[1:4] 输出结果：\", a[1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[786, 2.23]\n"
     ]
    }
   ],
   "source": [
    "#5.数据类型---List（列表）\n",
    "#列表是写在方括号 [] 之间、用逗号分隔开的元素列表。\n",
    "#列表索引值以 0 为开始值，-1 为从末尾的开始位置。\n",
    "list = ['abcd', 786 , 2.23, 'runoob', 70.2]\n",
    "print(list[1:3])\n",
    "\n",
    "#tinylist = [123, 'runoob']\n",
    "#print(list + tinylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'b', ['X', 'B'])\n"
     ]
    }
   ],
   "source": [
    "#5.数据类型---Tuple（元组）\n",
    "#tuple与list类似，不同之处在于tuple的元素不能修改。tuple写在小括号里，元素之间用逗号隔开。\n",
    "#元组的元素不可变，但可以包含可变对象，如list。\n",
    "t1 = ('abcd', 786 , 2.23, 'runoob', 70.2)\n",
    "t2 = (1, )\n",
    "t3 = ('a', 'b', ['A', 'B'])\n",
    "t3[2][0] = 'X'\n",
    "print(t3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "95\n"
     ]
    }
   ],
   "source": [
    "#5.数据类型---dict（字典）\n",
    "#字典是无序的对象集合，使用键-值（key-value）存储，具有极快的查找速度。\n",
    "#键(key)必须使用不可变类型。\n",
    "#同一个字典中，键(key)必须是唯一的。\n",
    "d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}\n",
    "print(d['Michael'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3}\n"
     ]
    }
   ],
   "source": [
    "#5.数据类型---set（集合）\n",
    "#set和dict类似，也是一组key的集合，但不存储value。由于key不能重复，所以，在set中，没有重复的key。\n",
    "#set是无序的，重复元素在set中自动被过滤。\n",
    "s = set([1, 1, 2, 2, 3, 3])\n",
    "print(s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 1.7.1 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
